package com.wbl.train.business.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.DailyTrainService;
import com.wbl.train.common.business.dto.DailyTrainDto;
import com.wbl.train.common.business.entity.DailyTrainEntity;
import com.wbl.train.common.business.vo.DailyTrainVo;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * @author 王博纶
 * @since 2024-02-10
 * @description 每日车次
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/admin/daily-train")
@Tag(name = "每日车次控制器类",
        description = "DailyTrainController 每日车次 后端数据接口")
public class DailyTrainAdminController {

    private final DailyTrainService dailyTrainService;

    /**
     * 通过id查询 火车每日车次
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query_all")
    public R<List<DailyTrainVo>> getAll() {
        List<DailyTrainVo> dailyTrainVos = dailyTrainService.queryAllDailyTrain();
        return R.ok("查询成功",dailyTrainVos);
    }

    /**
     * 通过id查询 火车每日车次
     * @param dailyTrainDto 火车每日车次Dto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<DailyTrainEntity>> getById(DailyTrainDto dailyTrainDto) {
        // 火车每日车次查询
        log.debug("trainDto :{}",dailyTrainDto);
        PageVo<DailyTrainEntity> pageVo = dailyTrainService.queryDailyTrain(dailyTrainDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 每日车次
     * @param dailyTrain 每日车次
     * @return Result
     */
    @Operation(summary = "新增每日车次", description = "新增每日车次")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody DailyTrainEntity dailyTrain) {
        return R.ok("保存成功",dailyTrainService.insertAndUpdateTrain(dailyTrain));
    }


    /**
     * 通过id删除 每日车次
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除每日车次",
            description = "通过id删除每日车次")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(dailyTrainService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前火车每日车次不存在");
        }
        return R.ok(dailyTrainService.removeById(id));
    }

    /**
     * 生成每日车次
     * @param date 生成车次的日期
     * @return Result
     */
    @Operation(summary = "生成每日车次",
            description = "生成每日车次")
    @GetMapping("/gen-daily/{date}")
    public R<Boolean> genTrain(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        dailyTrainService.genDaily(date);
        return R.ok(null);
    }

}
